package com.ihelp.rest.mis.platform.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ihelp.rest.base.hibernate4.BaseHibernateDao;
import com.ihelp.rest.base.po.Page;
import com.ihelp.rest.mis.pojo.Abnormaltitle;
import com.ihelp.rest.utils.JsonCommon;

@Repository
public class AbnormaltitleDao extends BaseHibernateDao<Abnormaltitle, String>{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<Abnormaltitle> getByUserCode(String usercode,String type){
		StringBuilder sb = new StringBuilder(); 
		sb.append(" from Abnormaltitle where ");
		sb.append("  type = '"+type+"'  "); 
		if(StringUtils.isNotBlank(usercode)){
			sb.append(" and usercode = '"+usercode+"'");
		}
		return this.list(sb.toString());
	}
	
	/**
	 * 异常上网时间查询
	 * @param queryCondition
	 * @param start
	 * @param pageSize
	 * @param sort
	 * @param order
	 * @return
	 */
	public Page<Abnormaltitle> queryAbnormaltitlePage(Abnormaltitle queryCondition,int start,int pageSize,String sort, String order){
		StringBuilder hql = new StringBuilder();
		ArrayList<Object> list = new ArrayList<Object>();
		hql.append("from Abnormaltitle t where 1=1 ");
		if(queryCondition.getType()!=null){
			hql.append(" and t.type=?");
			list.add(queryCondition.getType());
		}
		if(StringUtils.isNotBlank(queryCondition.getUsercode())){
			hql.append(" and t.usercode=?");
			list.add(queryCondition.getUsercode());
		}
		if(StringUtils.isNotBlank(sort))
			hql.append(" order by ").append(sort).append(" ").append(order);
		return this.page(hql.toString(), start, pageSize, (Object[])list.toArray());
	}
	
	@Transactional
	public void dropAbnormaltitle(String ids) throws Exception {
		try {
			NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(this.jdbcTemplate);
			MapSqlParameterSource parameters = new MapSqlParameterSource();  
			parameters.addValue("list", JsonCommon.idsTosList(ids, ","));
			
			StringBuilder sb = new StringBuilder();
			sb = new StringBuilder("DELETE Abnormaltitle T WHERE T.ID IN (:list)");
			namedParameterJdbcTemplate.update(sb.toString(), parameters);
		}
		catch (Exception e) {
			throw e;
		}
	}
}
